#!/usr/bin/env python
#  -*- coding: iso-8859-1 -*-
# Copyright (C) 2007-2022  CEA/DEN, EDF R&D
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#
# Author : Anthony GEAY (CEA/DEN/DM2S/STMF/LGLS)

from MEDLoader import MEDFileData,InterpKernelException
from CaseWriter import CaseWriter
from optparse import OptionParser
import os

parser = OptionParser()
parser.set_usage("Convert a MED file to a Case file.\n   %prog [options] med_file")
parser.add_option("-g", "--groups", action="store_true", dest="groups", default=False,
                  help="Are groups in meshes stored in MEDFile exported in output case as subparts (default False)")
parser.add_option("-c", "--currentdir", action="store_true", dest="here", default=False,
                  help="Are generated case,geo files generated in current directory. By default not, files are generated in directory containing the input file  (default False)")
(opts, args) = parser.parse_args()

if len(args) != 1:
    parser.print_usage()
    exit(1)
    pass
    
fname=os.path.abspath(args[0])  #"cas_test_simple.case"
if opts.here:
    fOut=os.path.splitext(os.path.basename(fname))[0]+".case"
    pass
else:
    fOut=os.path.splitext(fname)[0]+".case"
    pass
###
try:
    cw=CaseWriter.New()
    cw.setExportingGroups(opts.groups)
    mfd=MEDFileData(fname)
    cw.setMEDFileDS(mfd)
    listOfWrittenFileNames=cw.write(fOut)
except InterpKernelException as e:
    print("An error occurred during the conversion!")
    print("#######################################")
    raise e
print("#########")
for l in listOfWrittenFileNames:
    print("File \"%s\" successfully written !"%(l))
    pass
print("#########")
